package com.gxs.tga.poc.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gxs.tga.poc.domain.TgaCompany;
import com.gxs.tga.poc.domain.exceptions.DuplicateTgaCompany;
import com.gxs.tga.poc.domain.exceptions.InvalidClientRequest;
import com.gxs.tga.poc.rest.common.TgaRestResponse;
import com.gxs.tga.poc.service.TgaCompanyService;
import com.gxs.tga.poc.service.TgaCompanyService;

@Component("tgaCompanyRestBean")
@Path("/companies")
public class TgaCompanyRestBean
{
	@Autowired
	private TgaCompanyService tgaCompanyService;

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAllCompanies()
	{

		try
		{
			List<TgaCompany> list = this.tgaCompanyService.findAll();
			return Response.status(Status.OK).entity(list).build();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			int statusCode = 500;
			TgaRestResponse response = new TgaRestResponse(statusCode, "Error Occurred: " + e.getMessage(), statusCode);
			return Response.status(statusCode).entity(response).build();
		}
	}

	@GET
	@Path("/{compId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getSingleCompanyById(@PathParam("compId") String compId)
	{
		TgaCompany tgaCompany = this.tgaCompanyService.findById(compId);
		if (tgaCompany == null)
		{
			int statusCode = 404;
			TgaRestResponse response = new TgaRestResponse(statusCode, "No record of type 'TgaCompany' (Company) exists with the id: " + compId, statusCode);
			return Response.status(statusCode).entity(response).build();
		} else
		{
			return Response.status(200).entity(tgaCompany).build();
		}

	}

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response createOrUpdateCompany(TgaCompany company, @QueryParam("operation") String operation) throws InvalidClientRequest 
	{
		
		if(operation == null )
		{
			throw new InvalidClientRequest("Missing Query Parameter: 'operation' -- must be provided. Valid Values are: [create, update]");
		}
		else if(operation.equals("create") || operation.equals("update"))
		{
			// good
		}
		else
		{
			throw new InvalidClientRequest("Incorrect value for Query Parameter: 'operation' -- must be provided. Valid Values are: [create, update]");
		}
 
		if(operation.equals("update"))
		{
			throw new InvalidClientRequest("Work In Progress: Update operation is currently in development");
		}
		
		try
		{
			this.tgaCompanyService.create(company);
			
			int statusCode  = Response.Status.CREATED.getStatusCode();
			
			TgaRestResponse response = new TgaRestResponse(statusCode,"New instance of COMPANY created with id: " + company.getCompId() , -1);
			response.setTargetResource(company);
			response.setTargetResourceRefId(company.getCompId());
			response.setTargetResourcePKAttributeName("compId");
			
			return Response.status(statusCode).entity(response).build();
		}
		catch(DuplicateTgaCompany dte)
		{
			int statusCode  = Response.Status.CONFLICT.getStatusCode();
			TgaRestResponse response = new TgaRestResponse(statusCode, "Error: Company with id: " + company.getCompId() + " already exists" , statusCode);
			return Response.status(statusCode).entity(response).build();

		}
		
		
	}
 
}
